﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using AccessDAL;
using System.Collections.Generic;
using AccessModel;

namespace web
{
    public partial class CheckboxList : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            
                BindMenuNode();
           
           
        }

        private void BindMenuNode()
        {
            MenuDAL dal = new MenuDAL();
            List<AccessModel.Menu> listMenu = MenuDAL.QueryMenus(); ;
 

            this.MenuChbList.DataSource = listMenu;
            this.MenuChbList.DataTextField = "menuname"; //前台看到的值,也就是CheckBoxList中显示出来的值
            this.MenuChbList.DataValueField = "id"; //这个
            this.MenuChbList.DataBind();
            this.DropDownList1.DataSource = listMenu;
            this.DropDownList1.DataTextField = "menuname";
            this.DropDownList1.DataValueField = "id";
            this.DropDownList1.DataBind();
        }

        

        protected void GetAllCheckBox()
        {
            Role role = new Role();
            role.RoleName = this.txtRoleName.Text.Trim().ToString();
            RoleDAL rd = new RoleDAL();
            int id = rd.CreateNewRole(role);

            List<RoleMenu> listRoleMenu = new List<RoleMenu>();
            for (int i = 0; i < this.MenuChbList.Items.Count; i++)
            {
                RoleMenu rm = new RoleMenu();
                if (this.MenuChbList.Items[i].Selected == true)
                {
                    rm.RoleId = id;
                    rm.MenuId = Convert.ToInt32(this.MenuChbList.Items[i].Value.ToString());

                }
                listRoleMenu.Add(rm);
            }

            RoleMenuDAL rmDAL = new RoleMenuDAL();
            int j = rmDAL.CreateNewRoleMenu(listRoleMenu);
            if (j > 0)
            {
                ShowErrorMsg("Success");
            }
            else
            {
                ShowErrorMsg("Faild");
            }
        }

        protected void DropDownList1_DataBound(object sender, EventArgs e)
        {
            this.DropDownList1.Items.Insert(0, new ListItem("--请选择--", "0"));
        }

        public void ShowErrorMsg(string errorMsg)
        {
            ClientScript.RegisterStartupScript(this.GetType(), "", " <script language=javascript>alert('" + errorMsg + "') </script>");

        }

        protected void Button1_Click1(object sender, EventArgs e)
        {
            GetAllCheckBox();
        }
    }
}
